package com.rural.bus;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import cn.jpush.android.api.JPushInterface;

import com.rural.bus.model.User;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.utils.ProgressUtil;

public class RegisterActivity extends BaseActivity {
	/**从sd卡读取图片*/
//	public int PICK_REQUEST_CODE = 0;
	
	private EditText etPhoneNum, etUserpsw, etNickname;
	
	private String phonenum ;
	private String userpsw ;
	private String nickname ;
//	private String code;
	
//	private ImageView userimg;
	private String avatarPath;
	private Button btSubmit;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);

		initView();
        setListeners();
        
        initData();
	}

	@Override
	protected void initView() {
		
		etPhoneNum = (EditText) findViewById(R.id.et_phonenum);
		etUserpsw = (EditText) findViewById(R.id.userpsw);
		etNickname = (EditText) findViewById(R.id.et_nickname);
//		userimg = (ImageView) findViewById(R.id.userimg);
		btSubmit=(Button) findViewById(R.id.bt_submit);
		
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setText("用户注册");
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		
		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.back_bg_selector);
		
	}

	@Override
	protected void initData() {
//		TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
//		DEVICE_ID = tm.getDeviceId(); 
	}
	
	protected void setListeners(){
		btSubmit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				phonenum = etPhoneNum.getText().toString();
				userpsw = etUserpsw.getText().toString();
				nickname = etNickname.getText().toString();
//				code=etCode.getText().toString();
				
				if (TextUtils.isEmpty(phonenum) || TextUtils.isEmpty(userpsw) || 
						TextUtils.isEmpty(nickname)) {
					toast("请填写基本资料");
					return;
				}
				if(userpsw.length()<6){
					toast("密码最少为6个字符");
					return;
				}
				signUp();
			}
		});

//		userimg.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				getFileFromSD();
//			}
//		});
		
        mImgLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
        
	}
	
	/**
	 * 注册用户
	 */
	private void signUp() {
		ProgressUtil.showProgress(this, "");
		User user=new User();
		String regionId=JPushInterface.getRegistrationID(this);
		user.register(this,nickname,userpsw, phonenum, regionId, "", 0,new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				ProgressUtil.closeProgress();
				finish();
			}
			
			@Override
			public void fail(String error) {
				toast(error);
				ProgressUtil.closeProgress();
			}
		});
	}

}
